package com.tipfy.tmx;


class TiledTileSet
{
    /**
     * length > 0
     */
    public var name:String;

    /**
     * required, positiveInteger
     */
    public var firstgid:Int;

    /**
     * anyURI
     */
    public var source:String;

    /**
     * required, nonNegativeInteger
     */
    public var tilewidth:Int;

    /**
     * required, nonNegativeInteger
     */
    public var tileheight:Int;

    /**
     * nonNegativeInteger
     */
    public var spacing:Int;

    /**
     *
     */
    public var images:Array<TiledImage>;

    /**
     *
     */
    public var tiles:Array<TiledTile>;

    /**
     * Constructor.
     */
    public function new(xml:Xml)
    {
        // Attributes.
        this.name = TiledAttributes.getNonEmpty(xml, "name", null, false);
        this.firstgid = TiledAttributes.getPositiveInt(xml, "firstgid");
        this.source = TiledAttributes.get(xml, "source", null, false);
        this.tilewidth = TiledAttributes.getNonNegativeInt(xml, "tilewidth",
            null, false);
        this.tileheight = TiledAttributes.getNonNegativeInt(xml, "tileheight",
            null, false);
        this.spacing = TiledAttributes.getNonNegativeInt(xml, "spacing",
            null, false);

        // Child elements.
        for (elem in xml.elements())
        {
            switch (elem.nodeName)
            {
                case "image":
                    this._loadImage(elem);
                case "tile":
                    this._loadTile(elem);
                default:
                    throw "Invalid element <" + elem.nodeName +
                        "> in <tileset>";
            }
        }
    }

    /**
     * <image>. minOccurs="0", maxOccurs="unbounded"
     */
    private function _loadImage(xml:Xml):Void
    {
        if (this.images == null)
        {
            this.images = new Array<TiledImage>();
        }

        this.images.push(new TiledImage(xml));
    }

    /**
     * <tile>. minOccurs="0", maxOccurs="unbounded"
     */
    private function _loadTile(xml:Xml):Void
    {
        if (this.tiles == null)
        {
            this.tiles = new Array<TiledTile>();
        }

        this.tiles.push(new TiledTile(xml));
    }
}
